<template>
  <view class="rank-container">
    <view class="title">🏆 打卡排行榜</view>

    <view v-if="rankList.length === 0" class="empty-tip">暂无打卡数据</view>

    <view v-else>
      <view v-for="(item,index) in rankList" :key="index"
            class="rank-item"
            :class="{'top1':index===0,'top2':index===1,'top3':index===2}">
        <view class="left">
          <text class="rank-num">{{ getRankLabel(index) }}</text>
          <view class="avatar-wrapper">
            <image :src="item.avatar || '/static/images/default-avatar.png'" class="avatar"></image>
            <image v-if="index<3" :src="getCrown(index)" class="crown"></image>
          </view>
          <text class="user-name">{{ item.userName }}</text>
        </view>
        <view class="right">
          <text class="count">{{ item.clockCount }} 次</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "whoNb",
  data() {
    return {
      rankList: [],
    }
  },
  onLoad() {
    this.getRank()
  },
  methods: {
    getRank() {
      uni.showLoading({ title: '加载中...' });
      this.$request.get('/clock/rank')
          .then(res => {
            if (res.code === '200') {
              this.rankList = res.data;
            } else {
              uni.showToast({ title: res.msg || '加载失败', icon: 'none' });
            }
          })
          .catch(err => {
            console.error('排行榜加载失败:', err);
            uni.showToast({ title: '网络异常，请稍后重试', icon: 'none' });
          })
          .finally(() => {
            uni.hideLoading();
          });
    },
    getRankLabel(index) {
      switch(index) {
        case 0: return '冠军🥇';
        case 1: return '亚军🥈';
        case 2: return '季军🥉';
        default: return index + 1;
      }
    },
    getCrown(index) {
      const crowns = [
        '/static/images/crown1.png', // 冠军
        '/static/images/crown2.png', // 亚军
        '/static/images/crown3.png'  // 季军
      ];
      return crowns[index];
    }
  }
}
</script>

<style scoped>
.rank-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}
.title {
  font-size: 36rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 30rpx;
}
.rank-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24rpx;
  margin-bottom: 16rpx;
  border-radius: 24rpx;
  background: #fff;
  box-shadow: 0 6rpx 15rpx rgba(0,0,0,0.08);
  transition: transform 0.2s, box-shadow 0.2s;
}
.rank-item:hover {
  transform: translateY(-5rpx);
  box-shadow: 0 8rpx 18rpx rgba(0,0,0,0.1);
}
.left {
  display: flex;
  align-items: center;
}
.rank-num {
  font-size: 28rpx;
  font-weight: bold;
  width: 70rpx;
  text-align: center;
  color: #fff;
}
.avatar-wrapper {
  position: relative;
  margin: 0 15rpx;
}
.avatar {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  border: 3rpx solid transparent;
}
.crown {
  position: absolute;
  top: -10rpx;
  left: 50%;
  transform: translateX(-50%);
  width: 28rpx;
  height: 28rpx;
}
.user-name {
  font-size: 30rpx;
  font-weight: 500;
}
.count {
  font-size: 28rpx;
  color: #555;
  font-weight: 600;
}

/* 前三名样式 */
.top1 {
  background: linear-gradient(90deg,#ffd700,#fff7cc);
}
.top1 .rank-num { color: #000000; }
.top1 .avatar { border-color: #ffb800; }

.top2 {
  background: linear-gradient(90deg,#c0c0c0,#f2f2f2);
}
.top2 .rank-num { color: #000000; }
.top2 .avatar { border-color: #999; }

.top3 {
  background: linear-gradient(90deg,#cd7f32,#f9e0d0);
}
.top3 .rank-num { color: #000000; }
.top3 .avatar { border-color: #a56b40; }

.empty-tip {
  text-align: center;
  font-size: 28rpx;
  color: #999;
  margin-top: 60rpx;
}
</style>
